mui.init(); //页面初始化
var mask = mui.createMask(); //遮罩层
var city_leaber = "86";
var cityPicker3 = null;
var phone_type = "domestic";
//传入toggle参数，用户无需关心当前是显示还是隐藏状态，mui会自动识别处理；
//mui('#sheet1').popover('toggle');
//页面实际高度
$(document).ready(function() {
	$('body').css({
		'height': $(window).height()
	})
});

/*三级联动*/
(function($, doc) {
	$.init();
	$.ready(function() {
		/**
		 * 获取对象属性的值
		 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
		 * @param {Object} obj 对象
		 * @param {String} param 属性名
		 */
		var _getParam = function(obj, param) {
			return obj[param] || '';
		};
		if(city_leaber == "86" || city_leaber == "00886") {
			cityPicker3 = new $.PopPicker({
				layer: 3
			});
			cityPicker3.setData(cityData3);
			var showCityPickerButton = doc.getElementById('region');
			var cityResult3 = doc.getElementById('region');
			showCityPickerButton.addEventListener('tap', function(event) {
				cityPicker3.show(function(items) {
					cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		}
	});
})(mui, document);

//获取页面地址
var url = window.location.href;
//截取用户suer_id　　
var uid = url.substr(url.indexOf("?") + 1);

//调用发送验证码接口
$(document).on('tap', '#send_code', function() {
	var phone = $("#phone").val(); //获取电话号码
	//var phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/; //手机号验证
	if(!phone) {
		mui.toast('手机号不能为空');
		return
	}
	//	if(!phoneReg.test(phone)) {
	//		mui.toast('手机号格式不正确');
	//		return
	//	}
	mui.ajax('http://192.168.1.34:8080/api/verificationCode', {
		data: {
			"phone": phone,
			"sign": "string",
			"user_id": "",
			"type": "REGISTER_VERIFICATION_CODE"
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		headers: {
			'Content-Type': 'application/json'
		},
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.code == 10000) {
				function invokeSettime(obj) {
					var countdown = 60;
					settime(obj);
					console.log(JSON.stringify(data))

					function settime(obj) {
						if(countdown == 0) {
							$(obj).attr("disabled", false);
							$(obj).text("获取验证码");
							countdown = 60;
							return;
						} else {
							$(obj).attr("disabled", true);
							$(obj).text("重新发送(" + countdown + "s)");
							countdown--;
						}
						setTimeout(function() {
							settime(obj)
						}, 1000)
					}
				}
				new invokeSettime("#send_code");
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});
})

//点击确认测试
$(document).on('tap', '#sure_register_btn', function() {
	var phone = $("#phone").val(); //获取电话号码
	var verification_code = $("#verification_code").val(); //获取验证码
	var region = $("#region").text(); //获取注册地址
	var region_init = region.split(" "); //截取地区
	var receiver_state = region_init[0]; //省
	var receiver_city = region_init[1]; //市
	var receiver_district = region_init[2]; //区
	var registered_pwd = $("#registered_pwd").val(); //获取注册密码
	var registered_sure_pwd = $("#registered_sure_pwd").val(); //获取确认密码
	//var phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/; //手机号验证
	var pwdReg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,15}$/; //密码正则验证 
	if(!phone) {
		mui.toast('手机号不能为空');
		return
	}
	//	if(!phoneReg.test(phone)) {
	//		mui.toast('手机号格式不正确');
	//		return
	//	}
	if(!verification_code) {
		mui.toast('验证码不能为空');
		return
	}
	if(!registered_pwd) {
		mui.toast('密码不能为空');
		return
	}
	if(!pwdReg.test(registered_pwd)) {
		mui.toast('密码格式不正确');
		return
	}
	if(registered_pwd != registered_sure_pwd) {
		mui.toast('二次密码不一致');
		return
	}
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.ajax('http://192.168.1.34:8080/api/register', {
		data: {
			'phone': phone,
			'password': registered_pwd,
			'referral_code': uid,
			'rold': 2,
			'verification_code': verification_code,
			'sign': 2,
			"receiver_city": receiver_city,
			"receiver_district": receiver_district,
			"receiver_state": receiver_state,
			"source": 1
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		headers: {
			'Content-Type': 'application/json'
		},
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.code == 10000) {
				plus.nativeUI.closeWaiting(); //关闭等待
				mask.close(); //关闭遮罩层
				mui.alert('注册成功，请下载app登录')
			}

		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText);
			if(error_msg.msg == "手机号已注册") {
				return;
			} else {
				mui.toast(error_msg.msg)
			}
		}
	});
})

//点击跟换地区
$(document).on('tap', '.region_text', function() {
	var city_leabers = $(this).children("a").attr('leaber_num');
	$("#area_code").text("(+" + city_leabers + ")");
	if(city_leabers == "86") {
		phone_type = "domestic";
	} else {
		phone_type = "international";
	}
	/*三级联动*/
	(function($, doc) {
		$.init();
		$.ready(function() {
			/**
			 * 获取对象属性的值
			 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
			 * @param {Object} obj 对象
			 * @param {String} param 属性名
			 */
			var _getParam = function(obj, param) {
				return obj[param] || '';
			};

			if(city_leabers == "86") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData3);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "00886") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData5);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData4);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			}

		});
	})(mui, document);
})